package org.redmagic.agent.location.file;

import static org.redmagic.utils.Conditions.checkArgument;
import static org.redmagic.utils.Conditions.notNull;

import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;

import org.redmagic.agent.location.DomLocationParser;
import org.redmagic.location.UriLocation;
import org.w3c.dom.Element;

@Immutable
@ThreadSafe
public class FileLocationParser implements DomLocationParser {

    public static final String FILE_LOCATION_ELEMENT_TAG = "fileLocation";
    
    @Override
    public UriLocation parse(Element locationElement) {
        validateIncomingElement(locationElement);
        String uri = locationElement.getAttribute(URI_ATTRIBUTE);
        return new FileLocation(uri);
    }

    private void validateIncomingElement(Element locationElement) {
        notNull(locationElement, "locationElement");
        checkArgument(FILE_LOCATION_ELEMENT_TAG.equals(locationElement.getTagName()), 
                                 "Invalid element passed in.  Expecting one with a tagName of '"
                               + FILE_LOCATION_ELEMENT_TAG + "'.");
        checkArgument(locationElement.hasAttribute(URI_ATTRIBUTE), "uri must be present.");
        checkArgument(!locationElement.getAttribute(URI_ATTRIBUTE).isEmpty(), "uri cannot be empty.");
    }
    
    @Override
    public String toString() {
        return FILE_LOCATION_ELEMENT_TAG + "Parser";
    }
}